package com.goodsogood.union.auth.thrfit.client;

import java.io.IOException;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.thrift.TException;
import org.apache.thrift.TServiceClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.goodsogood.service.Filter;
import com.goodsogood.service.PageIndex;
import com.goodsogood.service.RID;
import com.goodsogood.service.Resp;
import com.goodsogood.service.RespCreate;
import com.goodsogood.service.union.user.thrift.AccountStatus;
import com.goodsogood.service.union.user.thrift.AuthService;
import com.goodsogood.service.union.user.thrift.RespCount;
import com.goodsogood.service.union.user.thrift.UserCompanyExtraResp;
import com.goodsogood.service.union.user.thrift.UserInfo;
import com.goodsogood.service.union.user.thrift.UserInfoDetail;
import com.goodsogood.service.union.user.thrift.UserInfoDetailResp;
import com.goodsogood.service.union.user.thrift.UserInfoResp;
import com.goodsogood.service.union.user.thrift.UserList;
import com.goodsogood.service.union.user.thrift.UserListItem;
import com.goodsogood.service.union.user.thrift.UserMoneyAccountResp;
import com.goodsogood.service.union.user.thrift.UserMoneyService;
import com.goodsogood.service.union.user.thrift.UserService;
import com.goodsogood.service.union.user.thrift.UserUniqueFilter;
import com.goodsogood.thrift.pool.connection.ThriftConnection;
import com.goodsogood.union.auth.thrfit.pool.UserConnectionPool;
import com.goodsogood.union.auth.util.UUIDUtils;

/**
 * 组织服务
 * Created by dongguochao on 26/10/2016.
 */
@Component
public class UserClient {

    @Autowired
    private UserConnectionPool userConnectionPool;

    private UserService.Client getClient(ThriftConnection<TServiceClient> conn) {
        return conn.getClient("userService", UserService.Client.class);
    }

    private AuthService.Client getAuthClient(ThriftConnection<TServiceClient> conn) {
        return conn.getClient("authService", AuthService.Client.class);
    }

    private UserMoneyService.Client getMoneyClient(ThriftConnection<TServiceClient> conn) {
        return conn.getClient("userMoneyService", UserMoneyService.Client.class);
    }

    private ThriftConnection<TServiceClient> conn() {
        return userConnectionPool.getConn();
    }

    private void close(ThriftConnection<TServiceClient> conn) {
        try {
            conn.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 搜索
     */
    public UserList query(String rId, Filter filter, PageIndex pageIndex) {
        ThriftConnection<TServiceClient> conn = conn();
        try {
            return getClient(conn).query(getHead(rId), filter, pageIndex);
        } catch (TException e) {
            e.printStackTrace();
            return null;
        } finally {
            close(conn);
        }
    }

    /**
     * 获取基本信息
     */
    public UserListItem get(String rId, UserUniqueFilter filter) throws TException {
        ThriftConnection<TServiceClient> conn = conn();
        try {
            return getClient(conn).get(getHead(rId), filter);
        } catch (TException e) {
            e.printStackTrace();
            return null;
        } finally {
            close(conn);
        }
    }

    /**
     * 获取基本信息
     */
//    public Resp flowIn(String rId, int tradetime, int amountC, String content, String outTradeNo, String userId) throws TException {
//        ThriftConnection<TServiceClient> conn = conn();
//        try {
//            return getMoneyClient(conn).flowIn(getHead(rId), tradetime, amountC, content, outTradeNo, userId);
//        } catch (TException e) {
//            e.printStackTrace();
//            return null;
//        } finally {
//            close(conn);
//        }
//    }
    
    /**
     * 增加
     */
    public Resp merge(String rId, String deadUserId, String liveUserId) throws TException {
        ThriftConnection<TServiceClient> conn = null;
        try {
            conn = conn();
            return getClient(conn).mergeAccount(getHead(rId), deadUserId, liveUserId);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        } finally {
            close(conn);
        }
    }
    
    /**
     * 获取基本信息
     */
    public Resp flowInByMch(String rId, int tradetime, int amountC, String content, String outTradeNo, String mchId, String userId, String type, String creatorName) throws TException {
        ThriftConnection<TServiceClient> conn = null;
        try {
            conn = conn();
            return getMoneyClient(conn).flowInByMch(getHead(rId), tradetime, amountC, content, outTradeNo, mchId, userId, type, creatorName);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        } finally {
            close(conn);
        }
    }

    /**
     * 获取基本信息
     */
    public Resp flowOut(String rId, int tradetime, int amountC, String content, String outTradeNo, String userId) throws TException {
        ThriftConnection<TServiceClient> conn = conn();
        try {
            return getMoneyClient(conn).flowOut(getHead(rId), tradetime, amountC, content, outTradeNo, userId);
        } catch (TException e) {
            e.printStackTrace();
            return null;
        } finally {
            close(conn);
        }
    }

    /**
     * 增加
     */
    public RespCreate add(String rId, UserInfo userInfo, UserInfoDetail userInfoDetail) throws TException {
        ThriftConnection<TServiceClient> conn = conn();
        try {
            return getClient(conn).createDetail(getHead(rId), userInfo, userInfoDetail);
        } catch (TException e) {
            e.printStackTrace();
            return null;
        } finally {
            close(conn);
        }
    }

    /**
     * 增加
     */
    public Resp forgetAccount(String rId, String userPhone, String userIdCard, String userName) throws TException {
        ThriftConnection<TServiceClient> conn = conn();
        try {
            return getClient(conn).forgetPhone(getHead(rId), userIdCard, userPhone, userName);
        } catch (TException e) {
            e.printStackTrace();
            return null;
        } finally {
            close(conn);
        }
    }

    /**
     * 增加
     */
    public Resp updateHead(String rId, String userId, String userHeadUrl) throws TException {
        ThriftConnection<TServiceClient> conn = conn();
        try {
            return getClient(conn).updateHead(getHead(rId), userId, userHeadUrl);
        } catch (TException e) {
            e.printStackTrace();
            return null;
        } finally {
            close(conn);
        }
    }

    /**
     * 增加
     */
    public UserCompanyExtraResp userCompanyExtra(String rId, String userId) throws TException {
        ThriftConnection<TServiceClient> conn = conn();
        try {
            return getClient(conn).userCompanyExtras(getHead(rId), userId);
        } catch (TException e) {
            e.printStackTrace();
            return null;
        } finally {
            close(conn);
        }
    }

    public RespCreate getUserIdByIdCard(String userIdCard){
        ThriftConnection<TServiceClient> conn = conn();
        try {
            return getClient(conn).getUserIdByIdCard(getHead(UUIDUtils.uuid32()), userIdCard);
        } catch (TException e) {
            e.printStackTrace();
            return null;
        } finally {
            close(conn);
        }
    }

    public RespCreate getUserIdByPhone(String userPhone){
        ThriftConnection<TServiceClient> conn = conn();
        try {
            return getClient(conn).getUserIdByPhone(getHead(UUIDUtils.uuid32()), userPhone);
        } catch (TException e) {
            e.printStackTrace();
            return null;
        } finally {
            close(conn);
        }
    }

    /**
     * 增加
     */
    public RespCreate update(String rId, String userId, UserInfo userInfo, UserInfoDetail userInfoDetail, List<String> properties) throws TException {
        ThriftConnection<TServiceClient> conn = conn();
        try {
            return getClient(conn).update(getHead(rId), userId, userInfo, userInfoDetail, properties);
        } catch (TException e) {
            e.printStackTrace();
            return null;
        } finally {
            close(conn);
        }
    }

    /**
     * 增加
     */
    public Resp userExistInCompany(String rId, String userId, String companyId) throws TException {
        ThriftConnection<TServiceClient> conn = conn();
        try {
            return getClient(conn).existInCompany(getHead(rId), userId, companyId);
        } catch (TException e) {
            e.printStackTrace();
            return null;
        } finally {
            close(conn);
        }
    }

    /**
     * 增加
     */
    public RespCreate addBasic(String rId, UserInfo userInfo) throws TException {
        ThriftConnection<TServiceClient> conn = conn();
        try {
            return getClient(conn).create(getHead(rId), userInfo);
        } catch (TException e) {
            e.printStackTrace();
            return null;
        } finally {
            close(conn);
        }
    }

    /**
     * 增加
     */
    public boolean isIdCardExists(String rId, String idCard) throws TException {
        ThriftConnection<TServiceClient> conn = conn();
        try {
            return getClient(conn).isIdCardExists(getHead(rId), idCard);
        } catch (TException e) {
            e.printStackTrace();
            return true;
        } finally {
            close(conn);
        }
    }

    /**
     * 增加
     */
    public Resp isIdCardExistsInCompany(String rId, String idCard, String companyId) throws TException {
        ThriftConnection<TServiceClient> conn = conn();
        try {
            return getClient(conn).isIdCardExistsInCompany(getHead(rId), idCard, companyId);
        } catch (TException e) {
            e.printStackTrace();
            return null;
        } finally {
            close(conn);
        }
    }

    /**
     * 批量增加
     */
    public Resp maintainUserCompanyExtraInfo(String rId, String companyId, String companyName, String groupId, String groupName, String userId) throws TException {
        ThriftConnection<TServiceClient> conn = conn();
        try {
            return getClient(conn).maintainUserCompany(getHead(rId), companyId, companyName, groupId, groupName, userId);
        } catch (TException e) {
            e.printStackTrace();
            return null;
        } finally {
            close(conn);
        }
    }

    public int payPwdStatus(String rId, String userId) {
        ThriftConnection<TServiceClient> conn = conn();
        try {
            return getMoneyClient(conn).payPwdStatus(getHead(rId), userId);
        } catch (TException e) {
            e.printStackTrace();
            return 0;
        } finally {
            close(conn);
        }
    }

    public Resp checkPayUserPwd(String rId, String userId, String pwd) {
        ThriftConnection<TServiceClient> conn = conn();
        try {
            return getMoneyClient(conn).checkPayPwd(getHead(rId), userId, pwd);
        } catch (TException e) {
            e.printStackTrace();
            return null;
        } finally {
            close(conn);
        }
    }

    public Resp updatePayPwd(String rId, String userId, String pwd) {
        ThriftConnection<TServiceClient> conn = conn();
        try {
            return getMoneyClient(conn).updatePayPwd(getHead(rId), userId, pwd);
        } catch (TException e) {
            e.printStackTrace();
            return null;
        } finally {
            close(conn);
        }
    }

    public Resp resetUserPwd(String rId, String userId, String newPwd, String salt) {
        ThriftConnection<TServiceClient> conn = conn();
        try {
            return getAuthClient(conn).resetUserPwd(getHead(rId), userId, newPwd, salt);
        } catch (TException e) {
            e.printStackTrace();
            return null;
        } finally {
            close(conn);
        }
    }

    public Resp checkUserPwd(String rId, String userId, String pwd) {
        ThriftConnection<TServiceClient> conn = conn();
        try {
            return getAuthClient(conn).checkUserPwd(getHead(rId), userId, pwd);
        } catch (TException e) {
            e.printStackTrace();
            return null;
        } finally {
            close(conn);
        }
    }

    public AccountStatus checkUserStatus(String rId, String userId) {
        ThriftConnection<TServiceClient> conn = conn();
        try {
            return getAuthClient(conn).checkUserStatus(getHead(rId), userId);
        } catch (TException e) {
            e.printStackTrace();
            return null;
        } finally {
            close(conn);
        }
    }

    public RespCreate chargeWXOrder(String rId, String userId, int amount, String openid, String outTradeNo) {
        ThriftConnection<TServiceClient> conn = conn();
        try {
            return getMoneyClient(conn).chargeWXOrder(getHead(rId), userId, amount, openid, outTradeNo);
        } catch (TException e) {
            e.printStackTrace();
            return null;
        } finally {
            close(conn);
        }
    }

    public RespCreate chargeAlipayOrder(String rId, String userId, int amount, String outTradeNo) {
        ThriftConnection<TServiceClient> conn = conn();
        try {
            return getMoneyClient(conn).chargeAlipayOrder(getHead(rId), userId, amount, outTradeNo);
        } catch (TException e) {
            e.printStackTrace();
            return null;
        } finally {
            close(conn);
        }
    }

    public Resp chargeWXCallback(String rId, String outTradeNo, String prepayId, int actualAmount, String openid, String callbackContent) {
        ThriftConnection<TServiceClient> conn = conn();
        try {
            return getMoneyClient(conn).chargeWXCallback(getHead(rId), outTradeNo, prepayId, actualAmount, openid, callbackContent);
        } catch (TException e) {
            e.printStackTrace();
            return null;
        } finally {
            close(conn);
        }
    }

    public Resp chargeAlipayCallback(String rId, String outTradeNo, String tradeNo, int actualAmount, String userEmail, String callbackContent) {
        ThriftConnection<TServiceClient> conn = conn();
        try {
            return getMoneyClient(conn).chargeAlipayCallback(getHead(rId), outTradeNo, tradeNo, actualAmount, userEmail, callbackContent);
        } catch (TException e) {
            e.printStackTrace();
            return null;
        } finally {
            close(conn);
        }
    }

    public UserInfoResp getUserInfo(String userId){
        ThriftConnection<TServiceClient> conn = conn();
        try {
            return getClient(conn).getUserInfo(getHead(UUIDUtils.uuid32()), userId);
        } catch (TException e) {
            e.printStackTrace();
            return null;
        } finally {
            close(conn);
        }
    }

    public UserInfoDetailResp getUserInfoDetail(String userId){
        ThriftConnection<TServiceClient> conn = conn();
        try {
            return getClient(conn).getUserInfoDetail(getHead(UUIDUtils.uuid32()), userId);
        } catch (TException e) {
            e.printStackTrace();
            return null;
        } finally {
            close(conn);
        }
    }

    public Resp resetUserInfoCache(String rId, String userId){
        ThriftConnection<TServiceClient> conn = conn();
        try {
            return getClient(conn).resetUserInfoCache(getHead(rId), userId);
        } catch (TException e) {
            e.printStackTrace();
            return null;
        } finally {
            close(conn);
        }
    }

    public Resp resetUserAccountCache(String rId, String userId){
        ThriftConnection<TServiceClient> conn = conn();
        try {
            return getClient(conn).resetUserAccountCache(getHead(rId), userId);
        } catch (TException e) {
            e.printStackTrace();
            return null;
        } finally {
            close(conn);
        }
    }

    public Resp resetUserCache(String rId, String userId){
        ThriftConnection<TServiceClient> conn = conn();
        try {
            return getClient(conn).resetUserCache(getHead(rId), userId);
        } catch (TException e) {
            e.printStackTrace();
            return null;
        } finally {
            close(conn);
        }
    }

    public RespCreate register(String rId, String userPhone, String pwd){
        ThriftConnection<TServiceClient> conn = conn();
        try {
            return getAuthClient(conn).reg(getHead(UUIDUtils.uuid32()), userPhone, pwd);
        } catch (TException e) {
            e.printStackTrace();
            return null;
        } finally {
            close(conn);
        }
    }

    public AccountStatus checkPhoneStatus(String userPhone){
        ThriftConnection<TServiceClient> conn = conn();
        try {
            return getAuthClient(conn).checkPhoneStatus(getHead(UUIDUtils.uuid32()), userPhone);
        } catch (TException e) {
            e.printStackTrace();
            return null;
        } finally {
            close(conn);
        }
    }

    public Resp verifyUserPhone(String rId, String userId){
        ThriftConnection<TServiceClient> conn = conn();
        try {
            return getAuthClient(conn).verifyUserPhone(getHead(rId), userId, null);
        } catch (TException e) {
            e.printStackTrace();
            return null;
        } finally {
            close(conn);
        }
    }


    public Resp bondMobilePhone(String rId, String userId, String phone){
        ThriftConnection<TServiceClient> conn = conn();
        try {
            return getClient(conn).bondMobilePhone(getHead(rId), userId, phone);
        } catch (TException e) {
            e.printStackTrace();
            return null;
        } finally {
            close(conn);
        }
    }

    public UserMoneyAccountResp getUserAccountInfo(String userId){
        ThriftConnection<TServiceClient> conn = conn();
        try {
            return getMoneyClient(conn).moneyAccount(getHead(UUIDUtils.uuid32()), userId);
        } catch (TException e) {
            e.printStackTrace();
            return null;
        } finally {
            close(conn);
        }
    }

    public Resp chargeGiftCard(String rId, String cardpwd, String userid) {
        ThriftConnection<TServiceClient> conn = conn();
        try {
            return getMoneyClient(conn).chargeGiftCard(getHead(rId), cardpwd, userid);
        } catch (TException e) {
            e.printStackTrace();
            return null;
        } finally {
            close(conn);
        }
    }

    public void logsLogin(String userId, String ip) {
        ThriftConnection<TServiceClient> conn = conn();
        try {
            getClient(conn).logsLogin(getHead(UUIDUtils.uuid32()), userId, ip);
        } catch (TException e) {
            e.printStackTrace();
        } finally {
            close(conn);
        }
    }

    /**
     *
     */
    public RespCreate updateWhenImport(String rId, UserInfo userInfo, UserInfoDetail detail, String companyId) throws TException {
        ThriftConnection<TServiceClient> conn = conn();
        try {
            return getClient(conn).updateWhenImport(getHead(rId), userInfo, detail, companyId);
        } catch (TException e) {
            e.printStackTrace();
            return null;
        } finally {
            close(conn);
        }
    }

    public Resp resetPhone(String rId, String userPhone){
        ThriftConnection<TServiceClient> conn = null;
        try {
            conn = conn();
            return getClient(conn).resetPhone(getHead(rId), userPhone);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        } finally {
            close(conn);
        }
    }
    
    /**
     * 获取基本信息
     */
    public Resp flowOutByMch(String rId, int tradetime, int amountC, String content, String outTradeNo, String mchId, String userId, String type, String creatorName) throws TException {
        ThriftConnection<TServiceClient> conn = null;
        try {
            conn = conn();
            return getMoneyClient(conn).flowOutByMch(getHead(rId), tradetime, amountC, content, outTradeNo, mchId, userId, type, creatorName);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        } finally {
            close(conn);
        }
    }
    private RID getHead(String rId) {
        if (StringUtils.isEmpty(rId)) {
            rId = UUIDUtils.uuid32();
        }
        RID head = new RID();
        head.setRId(rId);
        return head;
    }

	public RespCount getActivatedUserCount() {
		ThriftConnection<TServiceClient> conn = conn();
        try {
            return getClient(conn).queryAllActivatedUserCount(getHead(UUIDUtils.uuid32()));
        } catch (TException e) {
            e.printStackTrace();
            return null;
        } finally {
            close(conn);
        }
	}
}
